<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-row>
        <el-col :span="6">
          <el-button type="primary" plain @click="showAddCateDialog"
            >添加分类</el-button
          >
        </el-col>
      </el-row>
      <!-- 树形界面 -->
      <tree-table
        class="tree"
        :data="cateList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        index-text="*"
        border
        :show-row-hover="false"
      >
        <template slot="isOK" slot-scope="scope">
          <i
            class="el-icon-circle-check"
            v-if="scope.row.cat_deleted !== true"
            style="color:red"
          ></i>
          <i class="el-icon-circle-close" v-else></i>
        </template>
        <template slot="order" slot-scope="scope">
          <el-tag type="success" v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag type="warning" v-else-if="scope.row.cat_level === 1"
            >二级</el-tag
          >
          <el-tag type="danger" v-else>三级</el-tag>
        </template>
        <template slot="option">
          <el-button type="warning" size="small" icon="el-icon-edit" plain
            >编辑</el-button
          >
          <el-button type="danger" size="small" icon="el-icon-delete" plain
            >删除</el-button
          >
        </template>
      </tree-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="query.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <el-dialog
      title="添加分类"
      :visible.sync="addCateDialogVisible"
      width="40%"
      @close="deleteDialodData"
    >
      <!-- 内容 -->
      <el-form
        :model="addCateForm"
        :rules="addCateFormRules"
        ref="addCateRef"
        label-width="100px"
      >
        <el-form-item label="分类名称:" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级id:">
          <el-cascader
            v-model="selectArr"
            :options="cascaderList"
            :props="cascderOpt"
            @change="handleChange"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Cate',
  data () {
    return {
      // 请求数据参数配置
      query: {
        type: 3,
        pagenum: 1,
        pagesize: 3
      },
      cateList: [], // 商品分类数据列表
      total: 0, // 分类数
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name',
          align: 'center',
          headerAlign: 'center'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isOK',
          align: 'center',
          headerAlign: 'center'
        },
        {
          label: '排序',
          type: 'template',
          template: 'order',
          align: 'center',
          headerAlign: 'center'
        },
        {
          label: '操作',
          type: 'template',
          template: 'option',
          align: 'center',
          headerAlign: 'center'
        }
      ],
      addCateDialogVisible: false,
      // 添加分类数据对象
      addCateForm: {
        cat_name: '', // 名称
        cat_pid: 0, // 父级id
        cat_level: 0 // 分类等级
      },
      // 校验规则
      addCateFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      // 选择父级id
      selectArr: [],
      // 级联配置项
      cascderOpt: {
        expandTrigger: 'hover', // 展开方式
        value: 'cat_id', // 获取值
        label: 'cat_name', // 内容
        children: 'children', // 嵌套关系
        checkStrictly: true // 选中父级
      },
      // 选择分类的id
      cascaderList: []
    }
  },
  created () {
    this.getCateList()
  },
  methods: {
    // 获取商品分类列表
    async getCateList () {
      const { data: res } = await this.$http.get('categories', {
        params: this.query
      })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      // console.log(res)
      this.$message.success(res.meta.msg)
      this.cateList = res.data.result
      this.total = res.data.total
    },
    // 个数
    handleSizeChange (val) {
      this.query.pagesize = val
      this.getCateList()
    },
    // 页码
    handleCurrentChange (val) {
      this.query.pagenum = val
      this.getCateList()
    },
    // 级联选择器发生改变时触发
    handleChange () {
      if (this.selectArr.length > 0) {
        const index = this.selectArr.length - 1
        this.addCateForm.cat_pid = this.selectArr[index]
        this.addCateForm.cat_level = this.selectArr.length
      } else {
        // 一级为0
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      }
      console.log(this.selectArr)
    },
    // 显示添加分类对话框

    async showAddCateDialog () {
      const { data: res } = await this.$http.get('categories', {
        params: { type: 2 }
      })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      console.log(res)
      this.$message.success(res.meta.msg)
      this.cascaderList = res.data
      this.addCateDialogVisible = true
    },
    // 重置数据
    deleteDialodData () {
      this.selectArr = []
      this.addCateForm = {
        cat_level: 0,
        cat_pid: 0,
        cat_name: ''
      }
    },
    addCate () {
      this.addCateDialogVisible = false
      this.$refs.addCateRef.validate(async valid => {
        if (!valid) {
          return this.$message.error('检查分类名称')
        }
        const { data: res } = await this.$http.post(
          'categories',
          this.addCateForm
        )
        if (res.meta.status !== 201) return this.$message.error(res.meta.msg)
        this.getCateList()
        this.$message.success(res.meta.msg)
        this.addCateDialogVisible = false
      })
    }
  }
}
</script>

<style lang="less" scoped>
.tree {
  margin-top: 25px;
  font-size: 16px;
}
.el-cascader {
  width: 100%;
}
</style>
